import Vue from 'vue'
import VueRouter from 'vue-router'

// view组件
import Login from '@/views/Login/Login';
// import Home from '@/views/Home';
// import Welecom from '@/views/Main/Welecom';

// const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/views/Login/Login')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/views/Home')
const Welecom = () => import(/* webpackChunkName: "login_home_welcome" */ '@/views/Main/Welecom')

// import User from '@/views/Main/User/User';
const User = () => import(/* webpackChunkName: "user" */ '@/views/Main/User/User')


// import RightList from '@/views/Main/rightList/RightList';
// import RoleList from '@/views/Main/rightList/RoleList';
const RightList = () => import(/* webpackChunkName: "rightList_RoleList" */ '@/views/Main/rightList/RightList')
const RoleList = () => import(/* webpackChunkName: "rightList_RoleList" */ '@/views/Main/rightList/RoleList')

// import GoodsSort from '@/views/Main/goods_manage/GoodsSort';
// import GoodOptions from '@/views/Main/goods_manage/GoodOptions';
// import GoodList from '@/views/Main/goods_manage/GoodList';
// import AddGood from '@/views/Main/goods_manage/add_good/AddGood';
const GoodsSort = () => import(/* webpackChunkName: "sort_option_list_add" */ '@/views/Main/goods_manage/GoodsSort')
const GoodOptions = () => import(/* webpackChunkName: "sort_option_list_add" */ '@/views/Main/goods_manage/GoodOptions')
const GoodList = () => import(/* webpackChunkName: "sort_option_list_add" */ '@/views/Main/goods_manage/GoodList')
const AddGood = () => import(/* webpackChunkName: "sort_option_list_add" */ '@/views/Main/goods_manage/add_good/AddGood')

// import Order from '@/views/Main/order/Order';
const Order = () => import(/* webpackChunkName: "order" */ '@/views/Main/order/Order')

// import Report from '@/views/Main/report/Report';
const Report = () => import(/* webpackChunkName: "report" */ '@/views/Main/report/Report')
// view组件结束

Vue.use(VueRouter)

  const routes = [
    {path: '/', redirect: '/login'}, // 重定向
    {path: '/login', component: Login, 
      beforeEnter(to, from, next) { // 进入前路由独享守卫
        window.sessionStorage.removeItem('token'); // 删除sessionStorage
        next();
      }
    }, // 访问登陆组件
    {path: '/home', 
    component: Home,
    redirect: '/welecom',
    children: [
      {path: '/welecom', component: Welecom},
      {path: '/users', component: User},
      {path: '/rights', component: RightList},
      {path: '/roles', component: RoleList},
      {path: '/categories', component: GoodsSort},
      {path: '/params', component: GoodOptions},
      {path: '/goods', component: GoodList},
      {path: '/goods/add', component: AddGood},
      {path: '/orders', component: Order},
      {path: '/reports', component: Report}
    ]
  }, // 访问登陆组件
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  
  to.path === '/login' ? next() : ''; // 访问登陆页直接放行 
  window.sessionStorage.getItem('token') ? next() : next('/login'); // 其他页面如果有token放行
  
})

export default router;
